/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

var PlanetInfo = new Class({
  initialize: function(type, image, upkeep, production)
  {
    this.Type = type;
    this.Image = image;
    this.Upkeep = upkeep;
    this.Production = production;

    this.ClassName = "PlanetInfo";
  }
});
PlanetInfo.ClassName = "PlanetInfo";

var Planet = new Class({
  initialize: function(id, x, y, type, population)
  {
    this.ID = id;
    this.X = x;
    this.Y = y;
    this.Type = type;
    this.SetPopulation(population);

    this.ClassName = "Planet";
  },
  GetPopulation: function()
  {
    return this.population;
  },
  SetPopulation: function(value)
  {
    if (!$defined(value))
      value = 1;
      
    if ($type(value) == "string")
    {
      value = value.replace(/[^0-9]/g, "");
      if (value == "")
        value = 0;

      else
        value = value.toInt();
    }
    value = value.limit(500, 1000000);
    this.population = value;
    return value;
  },
  Info: function()
  {
    return Global.PlanetInfoList[this.Type];
  },
  Upkeep: function()
  {
    var result = new Hash();
    var upkeep = this.Info().Upkeep;

    for (commodity in upkeep)
    {
      if (upkeep.hasOwnProperty(commodity))
        result[commodity] = Math.round(this.GetPopulation() / 1000 * upkeep[commodity]);
    }

    return result;
  },
  Production: function()
  {
    var result = new Hash();
    var production = this.Info().Production;

    for (commodity in production)
    {
      if (production.hasOwnProperty(commodity))
        result[commodity] = Math.round(this.GetPopulation() / 1000 * production[commodity]);
    }

    return result;
  },
  DisplayName: function()
  {
    return "Class " + this.Type + " Planet";
  }
});
Planet.ClassName = "Planet";